﻿using System.Web.Mvc;
using Downloader.Domain.Contracts.Tasks;
using Downloader.Web.Controllers.Queries;

namespace Downloader.Web.Controllers
{
    public class ListController : Controller
    {
        public IArticleContentQuery ArticleContentQuery { get; set; }
        public ActionResult Index()
        {
         
          return View();
        }

        public ActionResult List(int categoryId, int pageIndex, int pageSize)
        {
            var result = ArticleContentQuery.GetList(categoryId, pageIndex, pageSize);
            ViewData["categoryId"] = categoryId;
            return PartialView(result);
        }

        public ActionResult ImgList(int categoryId,int pageIndex,int pageSize)
        {
            var result = ArticleContentQuery.GetImgList(categoryId, pageIndex, pageSize);
            ViewData["categoryId"] = categoryId;
            return PartialView(result);
        }

        public ActionResult ChangePage(ChangeParameter changeParameter)
        {
            int pageIndex = changeParameter.PageIndex;
            switch (changeParameter.PageChange)
            {
                    case PageChange.First:
                        pageIndex =  1;
                    break;
                    case PageChange.Up:
                    if (pageIndex > 1)
                         pageIndex --;
                    break;
                    case PageChange.Next:
                    if (pageIndex < changeParameter.PageCount)
                        pageIndex++;
                    break;
                    case PageChange.End:
                    pageIndex = changeParameter.PageCount;
                    break;
            }
            if(changeParameter.ListType==ListType.List){
            return this.List(changeParameter.CategoryId, pageIndex, changeParameter.PageSize);
            }
            return this.ImgList(changeParameter.CategoryId, pageIndex, changeParameter.PageSize);
        }

        public class ChangeParameter
        {
            public int CategoryId { get; set; }
            public int PageIndex { get; set; }
            public int PageSize { get; set; }
            public int PageCount { get; set; }
            public ListType ListType { get; set; }
            public PageChange PageChange { get; set; }
        }

        public enum ListType
        {
            List,
            ImgList
        }

        public enum PageChange
        {
            First,
            Up,
            Next,
            End
        }
    }
}
